package com.lingshu.app.base;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.lingshu.app.R;
import com.lingshu.app.config.App;
import com.lingshu.app.model.enums.ImagePart;
import com.lingshu.app.model.enums.ScreeningStatus;
import com.lingshu.app.model.enums.ScreeningType;
import com.lingshu.app.model.register.RegisterImageResponse;
import com.lingshu.core.utils.EasyDateUtils;
import com.lingshu.customview.photoview.PhotoView;

import java.io.File;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2023/12/29 17:13
 * @Description
 */
public class BindingAdapterUtil {
    /**
     * 设置ViewPager的数据列表
     *
     * @param viewPager ViewPager
     * @param dataList  数据列表
     * @param <T>       数据类型
     */
    @SuppressWarnings("unchecked")
    @BindingAdapter("dataList")
    public static <T> void setDataList(ViewPager viewPager, List<T> dataList) {
        PagerAdapter adapter = viewPager.getAdapter();
        if (adapter instanceof BasePagerAdapter) {
            BasePagerAdapter basePagerAdapter = (BasePagerAdapter) adapter;
            basePagerAdapter.setDataList(dataList);
        }
    }

    @BindingAdapter("int2String")
    public static void int2String(TextView textView, int i) {
        textView.setText("" + i);
    }

    @BindingAdapter("double2String")
    public static void double2String(TextView textView, double i) {
        textView.setText("" + i);
    }

    @BindingAdapter("doubleToString")
    public static void doubleToString(EditText text, double i) {
        text.setText("" + i);
    }

    @BindingAdapter("long2String")
    public static void long2String(TextView textView, long number) {
        textView.setText("" + number);
    }

    @BindingAdapter("age2String")
    public static void age2String(TextView textView, Integer age) {
        if (age != null) textView.setText("" + age);
        else textView.setText("未知");
        textView.setGravity(Gravity.RIGHT);
    }

    @BindingAdapter("gender2String")
    public static void gender2String(TextView textView, Integer gender) {
        if (gender != null) {
            switch (gender) {
                case 0:
                    textView.setText("未知");
                    break;
                case 1:
                    textView.setText("男");
                    break;
                case 2:
                    textView.setText("女");
                    break;
            }
        } else textView.setText("未知");
    }

    @BindingAdapter("parts2String")
    public static void parts2String(TextView textView, ImagePart parts) {
        if (parts != null) {
            switch (parts) {
                case NONE:
                    textView.setText("未知");
                    break;
                case ISTHMIC:
                    textView.setText("峡");
                    break;
                case LEFT:
                case LEFTLOBE:
                    textView.setText("左");
                    break;
                case RIGHT:
                case RIGHTLOBE:
                    textView.setText("右");
                    break;
            }
        } else textView.setText("未知");
    }

    @BindingAdapter("screeningType2String")
    public static void screeningType2String(TextView textView, ScreeningType screeningType) {
        if (screeningType == null) {
            textView.setText("未知");
        } else {
            switch (screeningType) {
                case NONE:
                    textView.setText("未知");
                    break;
                case THYROID:
                    textView.setText("甲状腺");
                    break;
                case CAROTID:
                    textView.setText("颈动脉");
                    break;
                case BREAST:
                    textView.setText("乳腺");
                    break;
                case ABDOMINAL:
                    textView.setText("腹部");
                    break;
                case URINARY:
                    textView.setText("泌尿系");
                    break;
                case PROSTATIC:
                    textView.setText("前列腺");
                    break;
                case VASCULAR:
                    textView.setText("外周血管");
                    break;
                case LYMPH:
                    textView.setText("浅表淋巴结");
                    break;
                case ARTERY:
                    textView.setText("四肢血管");
                    break;
                case HEART:
                    textView.setText("心脏");
                    break;
                case OBSTETRICS:
                    textView.setText("妇科");
                    break;
                case LUNG:
                    textView.setText("肺");
                    break;
                case SPINE:
                    textView.setText("脊柱");
                    break;
                case MULTIPLE:
                    textView.setText("多部位");
                    break;
            }
        }
    }

    @BindingAdapter("screeningStatus2String")
    public static void screeningStatus2String(TextView textView, ScreeningStatus screeningStatus) {
        if (screeningStatus == null) {
            textView.setText("未扫查");
        } else {
            switch (screeningStatus) {
                case NONE:
                    textView.setText("未扫查");
                    break;
                case SCANNING:
                    textView.setText("扫查中");
                    break;
                case IMAGEQA:
                    textView.setText("图像已质控");
                    break;
                case REPORTTOAUDIT:
                    textView.setText("报告待审核");
                    break;
                case REPORTCOMPLETED:
                    textView.setText("报告已完成");
                    break;
                case REPORTQA:
                    textView.setText("报告已质控");
                    break;
                case REPORTQAFAILED:
                    textView.setText("质控未通过");
                    break;
            }
        }
    }

    @BindingAdapter("loadImage")
    public static void setImageUrl(ImageView imageView, String url) {
        if (url != null) {
            Glide.with(imageView.getContext()).load(url).into(imageView);
        }
    }

    @BindingAdapter("loadImageWithError")
    public static void setImageUrlWithError(ImageView imageView, String url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.ic_logo).error(R.mipmap.ic_logo).fallback(R.mipmap.ic_logo).transform(new CircleCrop());

        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }

    @BindingAdapter("loadImageGoss")
    public static void setImageUrlWithGoss(ImageView imageView, String url) {
        Glide.with(imageView.getContext()).load(url).apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))).into(imageView);
    }

    public static void setImageUrl(ImageView imageView, RegisterImageResponse imageInfo) {
        if (imageInfo != null) {
            if (imageInfo.getCanPlay()) {

            }
        }
    }

    @BindingAdapter("visibility")
    public static void setViewVisibility(View view, Boolean visibility) {
        view.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    @BindingAdapter("todoGrade")
    public static void setTodoGrade(ImageView view, int priority) {
        if (priority == 0) {
            view.setImageResource(R.mipmap.ic_star);
        } else {
            view.setImageResource(R.mipmap.ic_star_outline);
        }
    }

    @BindingAdapter("todoCheck")
    public static void setToDoDone(CheckBox view, int status) {
        if (status == 0) {
            view.setChecked(false);
        } else {
            view.setChecked(true);
        }
    }

    @BindingAdapter("todoStatus")
    public static void setTodoStatus(TextView view, int status) {
        if (status == 0) {
            view.setText("未完成");
            view.setTextColor(App.getContext().getResources().getColor(R.color.app_color_theme_1));
        } else {
            view.setText("已完成");
            view.setTextColor(App.getContext().getResources().getColor(R.color.color_blue));
        }
    }

    @BindingAdapter("timeMillisToDateTime")
    public static void timeMillisToDateTime(TextView view, long timeMillis) {
        String dateTime = EasyDateUtils.stampToDate(timeMillis);
        view.setText(dateTime);
    }

    @BindingAdapter("imageViewBitmap")
    public static void setImageViewBitmap(ImageView imageView, String localStoragePath) {
        if (localStoragePath == null || StrUtil.isBlank(localStoragePath)) {
            imageView.setBackgroundColor(Color.BLACK);
        } else {
            File file = new File(localStoragePath);
            if (file.exists()) {
                imageView.setImageBitmap(BitmapFactory.decodeFile(localStoragePath));
            } else {
                imageView.setBackgroundColor(Color.BLACK);
            }
        }
    }

    @BindingAdapter("photoViewBitmap")
    public static void setPhotoViewBitmap(PhotoView imageView, String localStoragePath) {
        if (localStoragePath == null || StrUtil.isBlank(localStoragePath)) {
            imageView.setBackgroundColor(Color.BLACK);
        } else {
            File file = new File(localStoragePath);
            if (file.exists()) {
                imageView.setImageBitmap(BitmapFactory.decodeFile(localStoragePath));
            } else {
                imageView.setBackgroundColor(Color.BLACK);
            }
        }
    }

    @BindingAdapter("selectedReport")
    public static void setSelectedReport(ImageView imageView, Boolean flag) {
        if (flag == null) {
            imageView.setVisibility(View.GONE);
        } else if (flag == true) {
            imageView.setImageResource(R.drawable.selected);
            imageView.setVisibility(View.VISIBLE);
        } else {
            imageView.setImageResource(R.drawable.unselectd);
            imageView.setVisibility(View.VISIBLE);
        }
    }
}